﻿using GameCombine;
using GameMap;
using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using GameControl;

public class ObjectPanel : UIBase
{
    public List<Tuple<GameObject, CombinableItemField>> BenchItemList = new List<Tuple<GameObject, CombinableItemField>>();

    GridLayoutGroup grid;
    GameObject Basement;


    private void Awake()
    {


        grid = transform.GetComponent<GridLayoutGroup>();

     
        BenchItemList.Add(new Tuple<GameObject, CombinableItemField>( transform.Find("ObjectBasement").gameObject,null));

        GenCombineTable();
        MessageManager.Instance.Get<ItemMapEvent>(false).RegisterHandler(ItemMapEvent.BagToBench, Refresh);
        MessageManager.Instance.Get<ItemMapEvent>(false).RegisterHandler(ItemMapEvent.ChangeTransform, Refresh);
        MessageManager.Instance.Get<ItemMapEvent>(false).RegisterHandler(ItemMapEvent.DeleteInBench, Refresh);
        MessageManager.Instance.Get<ItemMapEvent>(false).RegisterHandler(ItemMapEvent.BenchToBag, Refresh);

    }
    EventArgs Refresh(object sender,EventArgs e)
    {
//if ((e as ItemMapArgs).UserName != CombineUI.Instance.UserName) return null;
        RefreshBench();
     //   CombineUI.Instance.ChangeSelectedObject(new Tuple<GameObject, ItemField>(CombineUI.Instance.GetPreview(CombineUI.Instance.draggingObj), (e as ItemInBagArgs).Item));
        return null;
    }
    /// <summary>
    /// 生成组装平台上的网格
    /// </summary>
    void GenCombineTable()
    {
        Vector3Int recSiz = CombineManager.Get<ICombineUIManager>().CombineTableSize;
        grid.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
        Vector2 siz = grid.cellSize;
        grid.constraintCount = recSiz.y;
        Vector2 panelSiz = new Vector2(BenchItemList[0].Item1.transform.parent.GetComponent<RectTransform>().rect.width,
        BenchItemList[0].Item1.transform.parent.GetComponent<RectTransform>().rect.height);
        grid.spacing = new Vector2((panelSiz.x - grid.padding.left * 2 - siz.x * recSiz.x) / (recSiz.x - 1),
            (panelSiz.y - grid.padding.top * 2 - siz.y * recSiz.y) / (recSiz.y - 1));
        grid.spacing = new Vector2(Mathf.Min(grid.spacing.x, grid.spacing.y), Mathf.Min(grid.spacing.x, grid.spacing.y));
        
        BenchItemList[0].Item1.name = "base_1_1";
     //   basementList[0].GetComponentInChildren<Button>().onClick.SetListener(() => ObjectOnClick("base_1_1"));
        for (int i = 1; i <= recSiz.x; i++)
        {
            for (int j = i == 1 ? 2 : 1; j <= recSiz.y; j++)
            {
                var tp = GameObject.Instantiate(BenchItemList[0].Item1, BenchItemList[0].Item1.transform.parent);
                tp.name = "base_" + i + "_" + j;
              //  tp.GetComponentInChildren<Button>().onClick.SetListener(() => ObjectOnClick(tp.name));
                BenchItemList.Add(new Tuple<GameObject, CombinableItemField>(tp,null));
            }
        }
        RefreshBench();
    }
    void RefreshBench()
    {
        var items = GameMap.GameMapManager.Get<GameMap.IBagManager>(CombineUI.Instance.UserName).ItemsInBench;
        for(int i = 0;i < BenchItemList.Count;i++)
        {
            var x = BenchItemList[i];
            var s =x.Item1.name.Split('_');
            x= new Tuple<GameObject, CombinableItemField>(x.Item1, items.Find(sss => sss.MeshTransform.BenchMeshPos == new Vector3Int(int.Parse(s[1]), int.Parse(s[2]), 1)));
            var tp = x.Item1.transform.Find("Image");
        //    Debug.Log(tp);
            x.Item1.transform.Find("Image").GetComponent<Image>().sprite = x.Item2 == null ? null : 
                GameManager.Get<IItemInstantiateManager>().GetItemSettingByName(x.Item2.ItemName.Split('_')[0]).GetComponentInChildren<SpriteRenderer>().sprite;

            //    Resources.Load<Sprite>("Texture/Item/" + (x.Item2==null?"":x.Item2.ItemName.Split('_')[0]));
            BenchItemList[i] = x;
            if (x.Item2!=null)
            {
                x.Item1.transform.eulerAngles = new Vector3(0, 0, x.Item2.MeshTransform.MeshRotate*90);
                x.Item1.transform.localScale = new Vector3(x.Item2.MeshTransform.IsFlipedInMesh ? -1 : 1, 1, 1);

            }
        }
    }
    /*   void ObjectOnClick(string id)
       {
           Debug.Log("Object=" + id);
           for(int i = 0; i < basementList.Count; i++)
           {
               if (basementList[i].name == id)
               {
                   this.transform.parent.GetComponent<CombineUI>().ChangeSelectedObject(basementList[i], null);
               }
           }
       }*/

}